package liming.ai.code.java_maven.info;

import liming.ai.code.java_maven.tool.ToJsonInterface;
import liming.tool.data.DataObject;
import liming.tool.file.FileRW;
import org.json.JSONObject;

import java.io.File;

public class ProjectInfo implements ToJsonInterface {
    @JsonField
    private final String mavenHome;
    @JsonField
    private final String mavenSettingPath;
    @JsonField
    private final String javaHome;
    @JsonField
    private final String ProjectPath;
    @JsonType(ModeInfo.class)
    @JsonField
    @JsonTime
    private final ModeInfo mainMode;

    public static ProjectInfo getInstance(File configFile) throws Exception {
        DataObject dataObject = DataObject.valueOfMap(
                new JSONObject(
                        FileRW.readTextFile(configFile)
                                .getContent()
                ).toMap()
        ).opt("codeConfig", DataObject.class, new DataObject());

        return new ProjectInfo(dataObject.opt("mavenHome")
                , dataObject.opt("mavenSettingPath")
                , dataObject.opt("javaHome")
                , dataObject.opt("ProjectPath"));
    }

    public ProjectInfo(String mavenHome, String mavenSettingPath, String javaHome, String ProjectPath) throws Exception {
        this.mavenHome = mavenHome;
        this.mavenSettingPath = mavenSettingPath;
        this.javaHome = javaHome;
        this.ProjectPath = ProjectPath;
        mainMode = ModeInfo.loadMode(this);
    }


    public String getMavenHome() {
        return mavenHome;
    }

    public String getJavaHome() {
        return javaHome;
    }

    public String getProjectPath() {
        return ProjectPath;
    }

    public String getMavenSettingPath() {
        return mavenSettingPath;
    }

    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mavenHome",getMavenHome());
        jsonObject.put("mavenSettingPath",getMavenSettingPath());
        jsonObject.put("javaHome",getJavaHome());
        jsonObject.put("ProjectPath",getProjectPath());
        jsonObject.put("mainMode",mainMode.toString());
        return new JSONObject().put("ProjectInfo",jsonObject).toString();
    }
}
